package com.llb666.e.shop.api.controller;

import com.llb666.e.shop.api.vo.CommentLevelCountsVo;
import com.llb666.e.shop.api.vo.ItemInfoVo;
import com.llb666.e.shop.api.vo.ShopCartVo;
import com.llb666.e.shop.domain.entity.Items;
import com.llb666.e.shop.domain.entity.ItemsImg;
import com.llb666.e.shop.domain.entity.ItemsParam;
import com.llb666.e.shop.domain.entity.ItemsSpec;
import com.llb666.e.shop.domain.entity.bo.ItemCommentBo;
import com.llb666.e.shop.domain.service.ItemService;
import com.llb666.e.shop.domain.service.ItemsEsService;
import com.llb666.e.shop.infracore.enums.CommentLevel;
import com.llb666.e.shop.infracore.web.BaseResponse;
import com.llb666.e.shop.infracore.web.DataResponse;
import com.llb666.e.shop.infracore.web.PagedGridResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * Created by 斌~
 * 2020/9/25 17:00
 *
 * @author mac
 */
@RestController
@Tag(name = "商品信息展示的相关接口")
@RequestMapping("/items")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ItemController {

    private final ItemService itemService;
    private final ItemsEsService itemsEsService;

    @Operation(summary = "查询商品详情信息")
    @GetMapping("/info/{itemId}")
    public BaseResponse queryById(@Parameter(name = "itemId", description = "商品Id", required = true)
                                  @PathVariable String itemId) {
        if (StringUtils.isBlank(itemId)) {
            return DataResponse.errorMap("商品id不能为空");
        }
        Items items = itemService.queryById(itemId);
        List<ItemsImg> itemsImgs = itemService.queryItemsImgById(itemId);
        List<ItemsSpec> itemsSpecs = itemService.queryItemsSpecById(itemId);
        ItemsParam itemsParam = itemService.queryItemsParamById(itemId);
        ItemInfoVo itemInfoVO = ItemInfoVo.convert(items,itemsImgs,itemsSpecs,itemsParam);
        return DataResponse.ok(itemInfoVO);
    }

    @Operation(summary = "查询商品评论等级")
    @GetMapping("/commentLevel")
    public BaseResponse queryCommentLevel(@Parameter(name = "itemId", description = "商品Id", required = true)
                                          @RequestParam String itemId) {
        if (StringUtils.isBlank(itemId)) {
            return DataResponse.errorMap("商品id不能为空");
        }
        Integer goodCommentCounts = itemService.getCommentCount(itemId, CommentLevel.GOOD.type);
        Integer normalCommentCounts = itemService.getCommentCount(itemId,CommentLevel.NORMAL.type);
        Integer badCommentCounts = itemService.getCommentCount(itemId,CommentLevel.BAD.type);
        Integer totalCommentCounts = goodCommentCounts + normalCommentCounts + badCommentCounts;

        CommentLevelCountsVo countsVo = new CommentLevelCountsVo(totalCommentCounts,goodCommentCounts,
                normalCommentCounts,badCommentCounts);
        return DataResponse.ok(countsVo);
    }

    @Operation(summary = "查询商品评论列表")
    @GetMapping("/comments")
    public BaseResponse queryCommentLevel(@Parameter(name = "itemId", description = "商品Id", required = true) @RequestParam String itemId,
                                          @Parameter(name = "level", description = "评论等级")  Integer level,
                                          @Parameter(name = "page", description = "页数")  Integer page,
                                          @Parameter(name = "pageSize", description = "每页显示条数") Integer pageSize) {
        if (StringUtils.isBlank(itemId)) {
            return DataResponse.errorMap("商品id不能为空");
        }
        if (page == null) {
            page = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        PagedGridResult<ItemCommentBo> pagedGridResult = itemService.queryItemCommentByPage(itemId, level, page, pageSize);
        return DataResponse.ok(pagedGridResult);
    }

    /**
     * 结合elasticsearch 查询数据
     */
    @Operation(summary = "搜索商品列表")
    @GetMapping("/search")
    public BaseResponse search(
            @Parameter(name = "keywords", description = "关键字", required = true) @RequestParam String keywords,
            @Parameter(name = "sort", description = "排序") @RequestParam String sort,
            @Parameter(name = "page", description = "查询下一页的第几页") Integer page,
            @Parameter(name = "pageSize", description = "分页的每一页显示的条数") Integer pageSize) {

        if (StringUtils.isBlank(keywords)) {
            return DataResponse.errorMsg(null);
        }
        if (page == null) {
            page = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        //根据keywords, sort, page, pageSize 去elasticsearch查询数据返回商品id列表
        page--;
        PagedGridResult<?> grid = itemsEsService.highlightSearchItems(keywords, sort, page, pageSize);//es高亮
//        PagedGridResult grid = itemsEsService.searchItems(keywords, sort, page, pageSize);//es
//        PagedGridResult grid = itemsEsService.queryItemsByPage(keywords, sort, page, pageSize);//mysql 查询
        return DataResponse.ok(grid);
    }

    @Operation(summary = "通过分类id搜索商品列表")
    @GetMapping("/catItems")
    public BaseResponse catItems(
            @Parameter(name = "catId", description = "三级分类id", required = true) @RequestParam Integer catId,
            @Parameter(name = "sort", description = "排序") @RequestParam String sort,
            @Parameter(name = "page", description = "查询下一页的第几页") Integer page,
            @Parameter(name = "pageSize", description = "分页的每一页显示的条数") Integer pageSize) {

        if (catId == null) {
            return DataResponse.errorMsg(null);
        }
        if (page == null) {
            page = 1;
        }

        if (pageSize == null) {
            pageSize = 10;
        }

        PagedGridResult<?> grid = itemService.searchItems(catId, sort, page, pageSize);

        return DataResponse.ok(grid);
    }

    /**
     * 用于用户长时间未登录网站，刷新购物车中的数据（主要是商品价格），类似京东淘宝
     *
     * @param itemSpecIds 拼接的规格ids
     * @return JSONResult
     */
    @Operation(summary = "刷新购物车中的数据（主要是商品价格）")
    @GetMapping("/refresh")
    public BaseResponse refresh(
            @Parameter(name = "itemSpecIds", description = "拼接的规格ids", required = true, example = "1001,1003,1005")
            @RequestParam String itemSpecIds) {
        if (StringUtils.isBlank(itemSpecIds)) {
            return BaseResponse.ok();
        }
        List<ShopCartVo> list = ShopCartVo.Converter.INSTANCE
                .bo2VoList(itemService.queryItemsBySpecIds(itemSpecIds));
        return DataResponse.ok(list);
    }
}
